//
//  QATableViewCell.m
//  CYWFrame
//
//  Created by Macx on 2017/10/25.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "QATableViewCell.h"
#import "Question.h"

#import "LZPictureBrowser.h"
#import <Masonry.h>
#import "PushPerHome.h"

@implementation QATableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self CreatUI];
    }
    
    return self;
}

- (void)CreatUI
{
    _headPic = [[UIImageView alloc] init];
    [self.contentView addSubview:_headPic];
    [_headPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(10);
        make.width.and.height.mas_offset(SW(9));
    }];
    _headPic.layer.cornerRadius = SW(18);
    _headPic.layer.masksToBounds = YES;
    
    _role = [[UIImageView alloc] init];
    [self.contentView addSubview:_role];
    [_role mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.mas_offset(0);
        //        make.right.mas_offset(0);
        make.bottom.mas_equalTo(_headPic.mas_bottom);
        make.right.mas_equalTo(_headPic.mas_right);
        make.width.and.height.mas_offset(SW(27));
    }];
    
    _nickName = [[UILabel alloc] init];
    [self.contentView addSubview:_nickName];
    _nickName.textColor = kBlackColor;
    _nickName.font = kUHSystemFontWithSize(16);
    [_nickName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headPic.mas_top).mas_offset(0);
        make.left.mas_equalTo(_headPic.mas_right).mas_offset(10);
        make.height.mas_offset(SW(16));
    }];
    
    _gender = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_gender];
    [_gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nickName.mas_top).mas_offset(0);
        make.left.mas_equalTo(_nickName.mas_right).mas_offset(5);
        make.size.mas_equalTo(CGSizeMake(SW(18), SW(18)));
    }];
    
    _date = [[UILabel alloc]init];
    _date.textColor = kLightGrayColor;
    _date.font = kUHSystemFontWithSize(12);
    [self.contentView addSubview:_date];
    [_date mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nickName.mas_bottom).mas_offset(0);
        make.left.mas_equalTo(_headPic.mas_right).mas_offset(10);
        make.height.mas_offset(SW(18));
    }];
    
    CGSize size3 = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(13)];
    _money = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_money];
//    [_money setImage:[IMAGE(@"icon_reward") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_money setBackgroundImage:IMAGE(@"icon_bg_f60") forState:UIControlStateNormal];
    [_money setTitleColor:kWhiteColor forState:UIControlStateNormal];
    _money.titleLabel.font = kUHSystemFontWithSize(13);
    [_money mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-10);
//        make.size.mas_equalTo(CGSizeMake(focusWidth, size3.height+8));
        make.centerY.mas_equalTo(_headPic.mas_centerY);
        make.height.mas_offset(size3.height+8);
    }];
    
    int state = [LZCTools thinkEnv];
    if (state == 0) {
        _money.hidden = YES;
    }

    
    _title = [[UILabel alloc]init];
    _title.textColor = kBlackColor;
    _title.font = kUHSystemFontWithSize(15);
    [self.contentView addSubview:_title];
    _title.numberOfLines = 2;
    [_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headPic.mas_bottom).mas_offset(10);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
    }];
    
    _bgView = [[UIView alloc] init];
    [self.contentView addSubview:_bgView];
    //    _bgView.backgroundColor = kRedColor;
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_title.mas_bottom).mas_offset(10);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_offset(0);
    }];
    
    
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < 3; i++) {
        UIImageView *img = [[UIImageView alloc] init];
        [_bgView addSubview:img];
        img.hidden = YES;
        img.contentMode = UIViewContentModeScaleAspectFit;
        [arr addObject:img];
    }
    self.imgArr = [NSArray arrayWithArray:arr];
    
//    _bottomView = [[UIView alloc] init];
//    [self.contentView addSubview:_bottomView];
//    //    _bgView.backgroundColor = kRedColor;
//    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_bgView.mas_bottom).mas_offset(0);
//        make.left.mas_offset(0);
//        make.right.mas_offset(0);
//        make.height.mas_offset(40);
//    }];
//    _bottomView.backgroundColor = kRedColor;
//    
    
    CGSize size2 = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(12)];
    
    _viewNum = [[UILabel alloc] init];
    [self.contentView addSubview:_viewNum];
    _viewNum.textColor = kLightGrayColor;
    _viewNum.font = kUHSystemFontWithSize(12);
    [_viewNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bgView.mas_bottom).offset(0);
        make.left.mas_offset(10);
        make.height.mas_offset(size2.height);
//        make.bottom.mas_offset(-10);
    }];
    
    _commentNum = [[UILabel alloc] init];
    [self.contentView addSubview:_commentNum];
    _commentNum.textColor = kLightGrayColor;
    _commentNum.font = kUHSystemFontWithSize(12);
    [_commentNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bgView.mas_bottom).offset(0);
        make.left.mas_equalTo(_viewNum.mas_right).offset(10);
        make.height.mas_offset(size2.height);
//        make.bottom.mas_offset(-10);
    }];
    
    self.qaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.qaBtn setImage:[IMAGE(@"icon_qna_write") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.qaBtn setTitle:@" 写回答" forState:UIControlStateNormal];
    self.qaBtn.titleLabel.font = kUHSystemFontWithSize(13);
    [self.qaBtn setTitleColor:RGB(69, 116, 159) forState:UIControlStateNormal];
    [self.contentView addSubview:self.qaBtn];
    [self.qaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_bgView.mas_bottom).offset(0);
        make.right.offset(-10);
        make.height.mas_offset(size3.height+8);
//        make.bottom.mas_offset(-10);
        make.centerY.mas_equalTo(_viewNum.mas_centerY);
//        make.width.offset(SW(4));
    }];
    
   
    _line = [[UIView alloc] init];
    [self.contentView addSubview:_line];
    _line.backgroundColor = UISeparatedColorRGB;
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_viewNum.mas_bottom).mas_offset(10);
        make.height.mas_offset(10);
        make.left.offset(0);
        make.width.mas_offset(SCREEN_WIDTH);
        make.bottom.mas_offset(0);
    }];
    
    

}

- (void)setModel:(Question *)model {
    if (_model != model) {
        _model = model;
        
        [self.headPic sd_setImageWithURL:[NSURL URLWithString:[model.author valueForKey:@"face"]] placeholderImage:IMAGE(@"placeHolder")];
        
        [self.headPic addActionWithblock:^{
            
            [PushPerHome pushPerHomeWithUserInfo:[model valueForKey:@"author"]];
            
        }];
        
        
        self.nickName.text = [model.author valueForKey:@"nickname"];
//        self.nickName.text = [NSString stringWithFormat:@"%ld", model.qId];
        if ([[model.author valueForKey:@"sex"] intValue] == 1) {
            [self.gender setImage:IMAGE(@"sex_boy") forState:UIControlStateNormal];
        }
        else
            [self.gender setImage:IMAGE(@"sex_girl") forState:UIControlStateNormal];
        
        if ([[model.author valueForKey:@"role"] isEqualToString:@"1"]) {
            self.role.image = IMAGE(@"");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"2"]) {
            self.role.image = IMAGE(@"icon_add_v");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"3"]) {
            self.role.image = IMAGE(@"icon_add_v_expert");
        }
        
        self.date.text = model.human_time;
        
//        if (model.title) {
//            self.title.text = model.title;
//        }
//        else
        
        self.title.text = model.body;
        
        NSMutableAttributedString *lsStr = [[NSMutableAttributedString alloc] initWithString:self.title.text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:4];//调整行间距
        [lsStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.title.text length])];
        self.title.attributedText = lsStr;
        
        if (model.reward == 0) {
            [self.money setTitle:@"未悬赏" forState:UIControlStateNormal];
        }
        else {
            [self.money setTitle:[NSString stringWithFormat:@"悬赏%.2f元", [[NSString stringWithFormat:@"%ld", model.reward] floatValue]/100] forState:UIControlStateNormal];
        }
        
        for (UIImageView *imgView in self.imgArr) {
            //            imgView.image = [UIImage imageNamed:@""];
            imgView.hidden = YES;
        }
        [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(0);
        }];
        
        if (model.pics.count > 0) {
            
            // 取屏
            self.mArr = [[NSMutableArray alloc] init];
            [self.mArr addObjectsFromArray:model.pics];
            NSArray *arr = model.pics;
            
            CGFloat imgW = (SCREEN_WIDTH-40)/3;
            for (int i = 0; i < arr.count; i++) {
                
                
                UIImageView *imageView = self.imgArr[i];
                imageView.hidden = NO;
                [imageView addTarget:self action:@selector(imageClick:)];
                [imageView sd_setImageWithURL:[NSURL URLWithString:arr[i]] placeholderImage:IMAGE(@"placeHolder")];
                imageView.tag = 1+i;
                imageView.contentMode = UIViewContentModeScaleAspectFill;
                imageView.clipsToBounds = YES;
                
                imageView.frame = CGRectMake((imgW+10)*i,0, imgW, imgW);
            }
            [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset(imgW+10);
            }];
            
        }
        self.viewNum.text = [NSString stringWithFormat:@"%ld浏览", model.views_count];
        if (model.answers_count) {
            self.commentNum.text = [NSString stringWithFormat:@"%ld评论", model.answers_count];
        }
        else {
            self.commentNum.text = [NSString stringWithFormat:@"%ld评论", model.comments_count];
        }
        
        [self updateConstraints];
        
    }
}

- (void)imageClick:(UIImageView *)img {
    
    UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer
                                             *)img;
    
    LZPictureBrowser *mvc = [[LZPictureBrowser alloc] init];
    [mvc showWithPictureURLs:self.mArr atIndex:[tapRecognizer.view tag]-1];
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
